const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const ExtractTextPlugin = require("extract-text-webpack-plugin")

module.exports = {
    entry:'./src/app.jsx',
    output: {
        path: path.resolve(__dirname,'dist'),
        publicPath:'/dist/',
        filename: 'js/app.js'
    },
    resolve: {
        alias: {
            page:path.resolve(__dirname,'src/page'),
            component:path.resolve(__dirname,'src/component')
        }
    },
    module: {
        rules: [
            {
                test: /\.jsx$/,
                exclude: /(node_modules)/,
                use:{
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env','@babel/preset-react']
                    }
                }
            },
            {
                test: /\.css$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: "css-loader"
                })
            },
            {
                test: /\.scss$/,
                use: ExtractTextPlugin.extract({
                    fallback: "style-loader",
                    use: ["css-loader","sass-loader"]
                })
            },
            {
                test: /\.(png|jpg|gif)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            // limit: 8192,
                            name: 'resource/[name].[ext]'
                        }
                    }
                ]
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2|otf)$/i,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            name: 'resource/[name].[ext]'
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
            favicon:'./favicon.ico'
        }),
        new ExtractTextPlugin("css/[name].css"),
    ],
    //提出公共模块
    optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    name: "commons",
                    filename: 'js/base.js',
                    chunks: "initial",
                    minChunks: 2
                }
            }
        }
    },
    devServer:{
        port:'8888',
        historyApiFallback: {
            index:'/dist/index.html'
        }
    },
    performance: {
        hints: false
    }
}